home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 62 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. From: David Byrden <100101.2547@compuserve.com>
  2. Message-ID: <4djps4$4h@news.bridge.net>
  3. X-Original-Date: 17 Jan 1996 21:31:48 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 18 Jan 96 02:35:04 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Worrying STL problem
  9. Organization: self-employed
  10. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMP2x6eEDnX0m9pzZAQHWXAF9GAC1ch53Ztf9HJJsVnFIzNRzmZlS6UmT
  13.     /f777ZFs6SsjQHFa1/6Xy+PsDLMG1YVv
  14.     =rjOi
  15.  
  16. I have run into a problem with the STL and Visual C++ 4, but I'm
  17. not sure whether the C++ language definition, or the STL 
  18. definition, or just Visual C++ itself is to blame.
  19.  
  20. I want to make a map of objects and key them on addresses. That's 
  21. not an unreasonable request; a map of memory blocks keyed on their 
  22. own addresses would be very useful if you wanted to concatenate them.
  23.  
  24. Here is a map declaration, using chars keyed on float addresses:
  25.  
  26. map< float*, char, less<float*> > theMap ;
  27.  
  28. Of course, when Microsoft get around to default template parameters, 
  29. I will be able to omit the functor    less<float*>
  30.  
  31. Now, let's use one of the member functions of map, to insert
  32. a char and its keying address; any address will illustrate
  33.  
  34. void f()
  35.  
  36. {
  37.  
  38.     float ff ;
  39.  
  40.     float* const pf = &ff ;
  41.  
  42.     theMap.insert( make_pair( pf, 'g' ) ) ;
  43.  
  44. }
  45.  
  46. This won't compile! The problem? the float address is not const, but 
  47. the code in the map template requires a const key object, i.e. it
  48. requires    pair< float* const, char >   
  49.  
  50. I have made pf a const float pointer, but that should not make a 
  51. difference in any case as we are passing a value.
  52.  
  53. The first problem arises with the library function make_pair(). It 
  54. takes const references to the objects you pass, but it returns a 
  55. pair<> object containing NON-CONST ones. So, is the STL at fault?
  56.  
  57. The second problem arises with the compiler. Visual C++ will not
  58. convert   pair< float* , char >    
  59. to the required   pair< float* const, char >
  60. That sounds reasonable to me, but again, is the compiler at fault?
  61.  
  62. Or does the definition of C++  ban that conversion, and of so, 
  63. is IT right to do so?
  64.  
  65. How is it SUPPOSED to work? Any relevant thoughts would be appreciated.
  66.  
  67.  
  68.           David
  69. ---
  70. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  71.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  72.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  73.